package rkdwogus7.swing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import java.awt.Font;

import javax.swing.JPasswordField;

import java.awt.Dimension;

import javax.swing.JRadioButton;

import rkdwogus7.dao.MemberDAO;
import rkdwogus7.model.MemberVO;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Arrays;

public class AddMember extends JDialog {

	
	private final JPanel contentPanel = new JPanel();
	private JTextField name;
	private JTextField uid;
	private JPasswordField repwd;
	private JPasswordField pwd;
	private MemberDAO mdao = new MemberDAO();
	// 저~ 아래 이벤트 처리부에 있던 애를 밖으로 빼 내서 쉽게 사용할 수 있게 한거야.
	// static을 이용하면 new 연산자 없이 객체를 바로 사용할 수 있음
	// 하지만, static 없이 사용하려면 클래스 수준의 멤버변수로 선언해서
	// 모든 method가 다 같이 사용할 수 있도록 할 수 있음
	// 나중에 select, update, delete 할 때 더 자세히 알아 볼 거야.
	// project에 ojdbc.jar file이 들어가야 사용 가능하게 돼. 잊지 마셩

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			AddMember dialog = new AddMember(null);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public AddMember(final JTable table) {
		setTitle(":: 회원가입 ::");
		setBounds(100, 100, 640, 480);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		
		JLabel txtName = new JLabel("이름");
		txtName.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		
		name = new JTextField();
		name.setColumns(10);
		
		JLabel txtUid = new JLabel("아이디");
		txtUid.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		
		uid = new JTextField();
		uid.setColumns(10);
		
		JLabel txtPwd = new JLabel("비밀번호");
		txtPwd.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		
		JLabel txtRepwd = new JLabel("비밀번호확인");
		txtRepwd.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		
		JLabel txtGender = new JLabel("성별");
		txtGender.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		
		repwd = new JPasswordField();
		repwd.setPreferredSize(new Dimension(100, 21));
		
		pwd = new JPasswordField();
		pwd.setPreferredSize(new Dimension(100, 21));
		
		JRadioButton isMale = new JRadioButton("남자");
		
		JRadioButton isFemale = new JRadioButton("여자");
		isFemale.setSelected(true);
		
		JButton checkUidBtn = new JButton("중복확인");
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING, false)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(txtPwd, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(pwd, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(txtRepwd, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(repwd, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(txtGender, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(isMale)
							.addGap(18)
							.addComponent(isFemale))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(txtUid, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
								.addComponent(txtName))
							.addGap(18)
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(uid, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
					.addGap(18)
					.addComponent(checkUidBtn)
					.addContainerGap(297, Short.MAX_VALUE))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(29)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtName)
						.addComponent(name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(11)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtUid, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)
						.addComponent(uid, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(checkUidBtn))
					.addGap(14)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtPwd, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)
						.addComponent(pwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(14)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtRepwd, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)
						.addComponent(repwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(14)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtGender, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)
						.addComponent(isMale)
						.addComponent(isFemale))
					.addContainerGap(207, Short.MAX_VALUE))
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton joinBtn = new JButton("가입완료");
				// 1. 가입완료에 익명 내부 클래스로 만들기
				joinBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						/*if (! pwd.getPassword().equals(repwd.getPassword())) {
							JOptionPane.showMessageDialog(null, "비밀번호가 일치하지 않아요!");
						} // getPassword()가 배열의 형태로 나타나기 때문에 비교할 수 없다*/
						// 비밀번호와 비밀번호 확인을 비교
						if ( ! Arrays.equals(pwd.getPassword(), repwd.getPassword()) ) {
							JOptionPane.showMessageDialog(null, "비밀번호가 일치하지 않아요!");
						}
						else {
							// MemberDAO mdao = new MemberDAO();
							// 매번 이렇게 객체 생성하려고 하면 귀찮잖아. 그러니 아예 객체를 선언해서 불러다 쓰면 돼
							// 그것도 아니면 static으로 쓰면 되고
							MemberVO m = new MemberVO(null, uid.getText(), new String ( pwd.getPassword() ), name.getText(), "", null);
							if (mdao.insertMember(m) >0 ) {
								JOptionPane.showMessageDialog(null, "회원가입완료!");
								// 이렇게 하면 값이 같을 경우 회원 가입을 시켜 주게 되는 거지
							}
							table.repaint();
						}
						
					}
				});
				joinBtn.setActionCommand("OK");
				buttonPane.add(joinBtn);
				getRootPane().setDefaultButton(joinBtn);
			}
		}
	}
}

// 1.여기 자료를 다 넣고 가입완료 누르면
// 데이터베이스에 저장하도록 할 거야






